#include <stdio.h>
#include "math.h"
#define M_PI 3.14159265358979323846

//定义被积函数
double f(double x)
{
	return x*x;
}

//辛普森算法
double simpsons_integral(double a, double b)
{
	int i;
	
	//计算步长
	double h = (b - a) / 1000;
	//初始化积分和
	double sum = f(a) + f(b);

	for (i = 1; i < 1000; i++)
	{
		double x = a + i * h;
		if (i % 2 == 0)
		{
			sum = sum + 2 * f(x);
		}
		else
		{
			sum = sum + 4 * f(x);
		}
	}
	return sum * h / 3;
}
